import axios from 'axios'
import qs from 'qs'

axios.defaults.timeout = 60000
axios.interceptors.request.use((config: object | any) => {
  const token = localStorage.getItem('MINITOOLBOX_tk')
  config.headers['Accept-Language'] = 'zh-cn'
  const url: string = config.url
  if(token && url.indexOf('uaa/oauth') < 0) {
    config.headers['Authorization'] = token
  }
  return config
}, error => {
  return Promise.reject(error)
})
export const UploadAction: string = process.env.NODE_ENV !== 'development'
  ? 'https://infotech-wiki.quectel.com/api/qos/oss/upload'
  : 'http://web-develop.quectel.com/qos/oss/upload';

export const LoginForm = () => {
  return {
    username: '',
    password: ''
  }
}

// minio上传
export const toUpload = (file: any) => {
  const formData = new FormData();
  formData.append('file', file);
  return axios.post(UploadAction, formData, {
    headers: {
      'Accept': '*/*',
      'Content-Type': 'multipart/form-data'
    },
  })
}

// 登录
export const toLogin = (params: any) => {
  let api = 'http://192.168.10.27:7010/api/uaa/oauth/token'
  if(process.env.NODE_ENV !== 'development') {
    api = 'https://sso-web.quectel.com/api/uaa/oauth/token'
  }
  return axios.post(api, qs.stringify(params))
}

// 翻译
export const getTranslate = (url: any) => {
  return axios.get(url)
}

/* --- --- */
const APPID = 'lsoqjbqnmiklvfhj'
const APPSECRET = 'U0JYTmVGSDAvTERTcXRHYVRXQmp3UT09'
// 油价
export const getArea = (area: string) => {
  let baseUrl = '/api/oil'
  if(process.env.NODE_ENV !== 'development') {
    baseUrl = 'https://www.mxnzp.com/api/oil'
  }
  const api = `${baseUrl}/search?province=${area}&app_id=${APPID}&app_secret=${APPSECRET}`
  return axios.get(api)
}
// 天气
export const getWater = (area: string) => {
  let baseUrl = '/api/weather'
  if(process.env.NODE_ENV !== 'development') {
    baseUrl = 'https://www.mxnzp.com/api/weather'
  }
  const api = `${baseUrl}/current/${area}?app_id=${APPID}&app_secret=${APPSECRET}`
  return axios.get(api)
}
// 搜图
export const searchImage = (msg: string, page: number ) => {
  let baseUrl = '/api/so-baidu-img/'
  if(process.env.NODE_ENV !== 'development') {
    baseUrl = 'https://zj.v.api.aa1.cn/api/so-baidu-img/'
  }
  const api = `${baseUrl}?msg=${msg}&page=${page}`
  return axios.get(api)
}
